package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.BaseDao;
import com.delivery.www.dao.DishDao;
import com.delivery.www.po.Collect;
import com.delivery.www.po.Dish;
import com.delivery.www.po.Thumb;
import com.delivery.www.util.JdbcUtils;
import com.mysql.cj.util.StringUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 12970
 */
public class DishDaoImpl implements DishDao {
    /**
     * 获取店铺下的菜品
     */
    @Override
    public List<Dish> getDish(int storeId, String dishName, int currentPageNo, int pageSize) {
        Connection connection = JdbcUtils.getConnection();
        List<Dish> dishes = null;
        if (connection != null) {
            StringBuilder sql = new StringBuilder();
            sql.append("select dish_id,dish_name,store_id,price,`describe` from dish where store_id = ?");
            List<Object> list = new ArrayList<>();
            list.add(storeId);
            if (!StringUtils.isNullOrEmpty(dishName)) {
                sql.append(" and dish_name like ?");
                list.add("%" + dishName + "%");
            }
            sql.append(" order by dish_id DESC limit ?,?");
            currentPageNo = (currentPageNo - 1) * pageSize;
            list.add(currentPageNo);
            list.add(pageSize);
            Object[] params = list.toArray();
            dishes = BaseDao.getList(connection, Dish.class, sql.toString(), params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return dishes;
    }

    @Override
    public int addDish(String dishName, int storeId, Float price, String describe) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {dishName, storeId, price, describe};
        String sql = "insert into dish (dish_name,store_id,price,`describe`) values (?,?,?,?)";
        int i = 0;
        if (connection != null) {
            i = BaseDao.execute(connection, sql, params);
            if (i > 0) {
                Object[] objects = {storeId};
                String s = "update store set dish_num = dish_num + 1 where store_id = ?";
                BaseDao.executeUpdate(connection, s, objects);
            }
            JdbcUtils.closeResource(connection, null, null);
        }
        return i;
    }

    @Override
    public int updateDishName(int dishId, String newName, int storeId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {newName, dishId, storeId};
        String sql = "update dish set dish_name = ? where dish_id = ? and store_id = ?";
        int excute = 0;
        if (connection != null) {
            excute = BaseDao.executeUpdate(connection, sql, params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return excute;
    }

    @Override
    public int updateDishPrice(int dishId, Float price, int storeId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {price, dishId, storeId};
        String sql = "update dish set price = ? where dish_id = ? and store_id = ?";
        int excute = 0;
        if (connection != null) {
            excute = BaseDao.executeUpdate(connection, sql, params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return excute;
    }

    @Override
    public int updateDishDescribe(int dishId, String introduce, int storeId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {introduce, dishId, storeId};
        String sql = "update dish set `describe` = ? where dish_id = ? and store_id = ?";
        int excute = 0;
        if (connection != null) {
            excute = BaseDao.executeUpdate(connection, sql, params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return excute;
    }

    @Override
    public int deleteDish(int dishId, int storeId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {dishId, storeId};
        String sql = "delete from dish where dish_id = ? and store_id = ?";
        int excute = 0;
        if (connection != null) {
            excute = BaseDao.executeUpdate(connection, sql, params);
            if (excute > 0) {
                Object[] objects = {storeId};
                String s = "update store set dish_num = dish_num -1 where store_id = ?";
                BaseDao.executeUpdate(connection, s, objects);
                String s1 = "delete from collect where dish_id = ?";
                BaseDao.executeUpdate(connection, s1, dishId);
                String s2 = "delete from thumb where dish_id = ?";
                BaseDao.executeUpdate(connection, s2, dishId);
                String s3 = "delete from cart_dish where dish_id = ?";
                BaseDao.executeUpdate(connection, s3, dishId);
            }
        }
        JdbcUtils.closeResource(connection, null, null);
        return excute;
    }

    @Override
    public Dish getOneDish(int dishId) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {dishId};
        Dish dish = null;
        String sql = "select dish_id,dish_name,store_id,price,`describe` from dish where dish_id = ?";
        if (connection != null) {
            dish = BaseDao.getForOne(connection, Dish.class, sql, params);
        }
        JdbcUtils.closeResource(connection, null, null);
        return dish;
    }

    @Override
    public List<Dish> getThumbDish(List<Thumb> thumbs) {
        Connection connection = JdbcUtils.getConnection();
        List<Integer> integers = new ArrayList<>();
        List<Dish> dishes = new ArrayList<>();
        for (Thumb thumb : thumbs) {
            Integer dishId = thumb.getDishId();
            integers.add(dishId);
        }
        Object[] params = integers.toArray(new Integer[0]);
        String sql = "select dish_id,dish_name,store_id,price,`describe` from dish where dish_id = ?";
        for (Object param : params) {
            Dish dish = BaseDao.getForOne(connection, Dish.class, sql, param);
            dishes.add(dish);
        }
        return dishes;
    }

    @Override
    public List<Dish> getCollectDish(List<Collect> collects) {
        Connection connection = JdbcUtils.getConnection();
        List<Integer> integers = new ArrayList<>();
        List<Dish> dishes = new ArrayList<>();
        for (Collect collect : collects) {
            Integer dishId = collect.getDishId();
            integers.add(dishId);
        }
        Object[] params = integers.toArray();
        String sql = "select dish_id,dish_name,store_id,price,`describe` from dish where dish_id = ?";
        for (Object param : params) {
            Dish dish = BaseDao.getForOne(connection, Dish.class, sql, param);
            dishes.add(dish);
        }
        return dishes;
    }

    @Override
    public int getDishCount(int storeId, String dishName) {
        Connection connection = JdbcUtils.getConnection();
        int count = 0;
        if (connection != null) {
            StringBuilder sql = new StringBuilder();
            sql.append("select count(1) count from dish where store_id = ?");
            List<Object> list = new ArrayList<>();
            list.add(storeId);
            if (!"".equals(dishName)) {
                sql.append(" and dish_name like ?");
                list.add("%" + dishName + "%");
            }
            Object[] params = list.toArray();
            count = BaseDao.selectCount(connection, sql.toString(), params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return count;
    }

    @Override
    public List<Dish> getDish(int storeId) {
        Connection connection = JdbcUtils.getConnection();
        List<Dish> dishes = null;
        if (connection != null) {
            String sql = "select dish_id,dish_name,store_id,price,`describe` from dish where store_id = ?";
            dishes = BaseDao.getList(connection, Dish.class, sql, storeId);
            JdbcUtils.closeResource(connection, null, null);
        }
        return dishes;
    }
}
